package cn.sms.order.service.impl;

import cn.sms.basic.service.impl.BaseServiceImpl;
import cn.sms.basic.util.CodeGenerateUtils;
import cn.sms.basic.util.PageList;
import cn.sms.order.domain.AdoptOrder;
import cn.sms.order.domain.OrderAddress;
import cn.sms.order.domain.ProductOrder;
import cn.sms.order.mapper.OrderAddressMapper;
import cn.sms.order.mapper.ProductOrderMapper;
import cn.sms.order.query.ProductOrderQuery;
import cn.sms.order.service.IProductOrderService;
import cn.sms.org.domain.Employee;
import cn.sms.org.mapper.EmployeeMapper;
import cn.sms.pay.constants.PayConstant;
import cn.sms.pay.domain.PayBill;
import cn.sms.pay.mapper.PayBillMapper;
import cn.sms.pay.service.IPayBillService;
import cn.sms.product.domain.Product;
import cn.sms.product.mapper.ProductMapper;
import cn.sms.quartz.constant.JobConstants;
import cn.sms.quartz.service.IQuartzService;
import cn.sms.quartz.util.QuartzJobInfo;
import cn.sms.user.domain.Logininfo;
import cn.sms.user.domain.User;
import cn.sms.user.domain.UserAddress;
import cn.sms.user.mapper.UserAddressMapper;
import cn.sms.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class ProductOrderServiceImpl extends BaseServiceImpl<ProductOrder> implements IProductOrderService {
    @Autowired
    private ProductOrderMapper productOrderMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private UserMapper userMapper;
    @Value("${order.last.pay.time}")
    private Integer lastPayTime;
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Autowired
    private PayBillMapper payBillMapper;
    @Autowired
    private IQuartzService quartzService;
    @Autowired
    private IPayBillService iPayBillService;


    @Override
    public PageList<ProductOrder> admin(ProductOrderQuery query, Logininfo loginInfo) {
        Employee employee = employeeMapper.ByLogininfoId(loginInfo.getId());
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        return super.queryPage(query);
    }

    @Override
    public String submit(Map<String, Object> params, Logininfo loginInfo) {
        Integer serviceMethod = Integer.valueOf(params.get("service_method").toString());
        Long addressId = Long.valueOf(params.get("address_id").toString());
        Long payMethod = Long.valueOf(params.get("pay_method").toString());
        Long petId = Long.valueOf(params.get("product_id").toString());

        Product product = productMapper.loadById(petId);
        User user = userMapper.ByLogininfoId(loginInfo.getId());
        ProductOrder productOrder = initProductOrder(product, user);
        productOrderMapper.save(productOrder);

        UserAddress userAddress = userAddressMapper.loadById(addressId);
        OrderAddress orderAddress = initOrderAddress(productOrder, userAddress);
        orderAddressMapper.save(orderAddress);

        PayBill payBill = initPayBill(payMethod, product, user, productOrder);
        payBillMapper.save(payBill);

        QuartzJobInfo quartzJobInfo = new QuartzJobInfo();
        String jobName = JobConstants.TIME_OUT_PRODUCT_LAST_PAY+productOrder.getId();
        quartzJobInfo.setJobName(jobName);
        quartzJobInfo.setFireDate(productOrder.getLastPayTime());
        Map map = new HashMap<>();
        map.put("type", JobConstants.TIME_OUT_PRODUCT_LAST_PAY);
        map.put("orderId", productOrder.getId());
        quartzJobInfo.setParams(map);
        quartzService.addJob(quartzJobInfo);

        return iPayBillService.pay(payBill);
    }

    @Override
    public void cancelByQuartz(Long orderId) {
        ProductOrder productOrder = productOrderMapper.loadById(orderId);
        if(productOrder != null){
            if(productOrder.getState() == 0){
                productOrder.setState(-1);
                productOrderMapper.update(productOrder);
            }
        }
    }

    @Override
    public ProductOrder queryByOrderSn(String orderSn) {
        return productOrderMapper.queryByOrderSn(orderSn);
    }

    private PayBill initPayBill(Long payMethod, Product product, User user, ProductOrder productOrder) {
        PayBill bill = new PayBill();
        bill.setDigest("[服务订单]购买"+product.getSalecount()+"次"+product.getName()+"的服务购买支付订单");
        bill.setMoney(productOrder.getPrice());
        bill.setUnionPaySn(productOrder.getPaySn());
        bill.setState(0);
        bill.setLastPayTime(productOrder.getLastPayTime());
        bill.setPayChannel(payMethod);
        bill.setBusinessType(PayConstant.BUSINESS_TYPE_PRODUCT);
        bill.setBusinessKey(productOrder.getId());
        bill.setUser_id(productOrder.getUser_id());
        bill.setShop_id(productOrder.getShop_id());
        bill.setNickName(user.getUsername());
        return bill;
    }

    private OrderAddress initOrderAddress(ProductOrder productOrder, UserAddress userAddress) {
        OrderAddress orderAddress = new OrderAddress();
        BeanUtils.copyProperties(userAddress, orderAddress);
        orderAddress.setOrder_id(productOrder.getId());
        orderAddress.setOrderSn(productOrder.getOrderSn());
        return orderAddress;
    }

    private ProductOrder initProductOrder(Product product, User user) {
        ProductOrder productOrder = new ProductOrder();
        productOrder.setDigest("[服务订单]购买"+product.getSalecount()+"次"+product.getName());
        productOrder.setState(0);
        /*BigDecimal getSalecount = new BigDecimal(product.getSalecount());*/
        /*BigDecimal price = product.getSaleprice().multiply(getSalecount);*/
        productOrder.setPrice(product.getSaleprice());
        productOrder.setOrderSn(CodeGenerateUtils.generateOrderSn(user.getId()));
        productOrder.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        productOrder.setLastPayTime(new Date(System.currentTimeMillis()+lastPayTime));
        productOrder.setProduct_id(product.getId());
        productOrder.setUser_id(user.getId());
        productOrder.setShop_id(product.getShop_id());
        return productOrder;
    }
}
